package com.lgcns.android2;

import java.io.IOException;
import java.util.HashMap;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class ProfileProvider extends ContentProvider {

    private static final String TAG = MainActivity.TAG;

    // TO DO : DB 관련 상수는 여기에 지정하자.
    public static final String[] PROJECTION_PHOTO = {
        Constants.ID_NUM,
        Constants.PHOTO,
        };

    public static final String PROFILE_TABLE_NAME = "profile";
    public static final String TEAM_TABLE_NAME = "team";
    public static final String AUTHORITY = "com.lgcns.android2";

    public static final Uri PROFILES_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/profiles");
    public static final Uri TEAM_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/team");

    private DatabaseHelper mOpenHelper;
    private static HashMap<String, String> sProfilesProjectionMap;
    private static HashMap<String, String> sTeamProjectionMap;
    private static final UriMatcher sUriMatcher;
    
    private static final int PROFILES = 1;
    private static final int PROFILE_ID = 2;
    private static final int TEAM = 3;
    
    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITY, "profiles", PROFILES);
        sUriMatcher.addURI(AUTHORITY, "profiles/#", PROFILE_ID);
        sUriMatcher.addURI(AUTHORITY, "team", TEAM);

        sProfilesProjectionMap = new HashMap<String, String>();
        sProfilesProjectionMap.put(Constants._ID, Constants._ID);
        sProfilesProjectionMap.put(Constants.ID_NUM, Constants.ID_NUM);
        sProfilesProjectionMap.put(Constants.NAME, Constants.NAME);
        sProfilesProjectionMap.put(Constants.TITLE_CODE, Constants.TITLE_CODE);
        sProfilesProjectionMap.put(Constants.TITLE, Constants.TITLE);
        sProfilesProjectionMap.put(Constants.TEL, Constants.TEL);
        sProfilesProjectionMap.put(Constants.EMAIL, Constants.EMAIL);
        sProfilesProjectionMap.put(Constants.LOCATION, Constants.LOCATION);
        sProfilesProjectionMap.put(Constants.JOB, Constants.JOB);
        sProfilesProjectionMap.put(Constants.PHOTO, Constants.PHOTO);
        sProfilesProjectionMap.put(Constants.MANAGER, Constants.MANAGER);

        sTeamProjectionMap = new HashMap<String, String>();
        sTeamProjectionMap.put(Constants.TEAM_DESC, Constants.TEAM_DESC);
        sTeamProjectionMap.put(Constants.TEAM_ORGANIZATION, Constants.TEAM_ORGANIZATION);
    }
    
    @Override
    public boolean onCreate() {
        mOpenHelper = new DatabaseHelper(getContext());
        
//        try {
//            Log.d("###", "createDataBase in provider");
//
//            mOpenHelper.createDataBase();
//        } catch (IOException ioe) {
//            throw new Error("Unable to create database");
//        }

//        try {
//            mOpenHelper.openDatabase();
//        }catch(SQLException sqle){
//            throw sqle;
//        }
        
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String orderBy) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        switch (sUriMatcher.match(uri)) {
            case PROFILES:
                qb.setTables(PROFILE_TABLE_NAME);
                qb.setProjectionMap(sProfilesProjectionMap);
                break;
            case PROFILE_ID:
                qb.setTables(PROFILE_TABLE_NAME);
                qb.setProjectionMap(sProfilesProjectionMap);
                qb.appendWhere(Constants.ID_NUM + "=" + uri.getPathSegments().get(1));
                break;
            case TEAM:
                qb.setTables(TEAM_TABLE_NAME);
                qb.setProjectionMap(sTeamProjectionMap);
            default:
                break;
        }

        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy);
        c.setNotificationUri(getContext().getContentResolver(), uri);

        return c;
    }

    @Override
    public int delete(Uri arg0, String arg1, String[] arg2) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)){
            case PROFILES:
                return "vnd.android.cursor.dir/vnd.android2.profiles";
            case PROFILE_ID:
                return "vnd.android.cursor.item/vnd.android2.profile";
            default:
                throw new IllegalArgumentException("Unsupported URI : " + uri);
        }
    }

    @Override
    public Uri insert(Uri arg0, ContentValues arg1) {
        return null;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

}
